﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NewsComp;

namespace DataIssue.Html
{
    public class PageSourceHtml
    {
        /// <summary>
        /// 获取指数行情字符串
        /// </summary>
        /// <returns></returns>
        public string GetIndexDataString()
        {
            string _cacheKey = "IndexDatasKeyDataIssue";
            object obj = CacheManager.GetCache(_cacheKey);
            if (obj == null)
            {
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                Service.IndexDataService service = new Service.IndexDataService();
                List<Entity.IndexData> datas = service.GetIndexDatas();
                foreach (Entity.IndexData data in datas)
                {
                    sb.AppendFormat("<a href=\"{0}\" id=\"pb_sh_{1}\" class=\"{8}\"><span class=\"q1\">{2}</span><span id=\"index_{3}\" class=\"q2 {6}\">{4}&nbsp;&nbsp;{7}{5}%</span></a>", GetURL(data.Code), data.Code, data.Name, data.Code, data.Value, data.Range, GetColor(data.Code, data.Range), GetForce(data.Range), GetClass(data.Code));
                }
                string html = sb.ToString();
                CacheManager.AddCache(_cacheKey, html, 1);
                return html;
            }
            else
            {
                return obj as string;
            }
        }


        /// <summary>
        /// 获取指数行情字符串
        /// </summary>
        /// <returns></returns>
        public string[] GetPicIndexString()
        {
            string _cacheKey = "PicIndexDataIssue";
            object obj = CacheManager.GetCache(_cacheKey);
            if (obj == null)
            {
                string[] picIndexs = new string[3];
                List<Entity.IndexData> datas = new Service.IndexDataService().GetPicIndexDatas();

                for (int i = 0; i < datas.Count; i++)
                {
                    picIndexs[i] = "<div><span class=\"" + GetColor(datas[i].Range) + "\">" + string.Format("{0:N2}", datas[i].Value) + "&nbsp;&nbsp;</span> 涨跌幅：<span class=\"" + GetColor(datas[i].Range) + "\">" + string.Format("{0:N2}", datas[i].Range) + "%</span> 成交额：" + string.Format("{0:N2}", (Convert.ToDecimal(datas[i].AllAmount) / 100000000)) + "亿</div>";
                }

                CacheManager.AddCache(_cacheKey, picIndexs, 1);
                return picIndexs;
            }
            else
            {
                return obj as string[];
            }
        }


        /// <summary>
        /// 获取市场规模字符串
        /// </summary>
        /// <returns></returns>
        public string GetMarketScaleString()
        {
            string _cacheKey = "MarketScalesKeyDataIssue";
            object obj = CacheManager.GetCache(_cacheKey);
            if (obj == null)
            {
                System.Text.StringBuilder sb = new System.Text.StringBuilder("<tbody id=\"sczt_tbody\">");
                List<Entity.MarketScale> lists = new Service.MarketScaleService().GetMarketScales();
                for (int i = 0; i < lists.Count; i++)
                {
                    sb.AppendFormat("<tr class=\"{0}\">", i % 2 == 0 ? "tr1" : "tr2");
                    sb.AppendFormat("<td class=\"leftblock\">{0}</td>", lists[i].Name);
                    sb.AppendFormat("<td align=\"right\">{0:N2}</td>", lists[i].ValueA);
                    sb.AppendFormat("<td align=\"right\">{0:N2}</td>", lists[i].ValueB);
                    sb.Append("</tr>");
                }
                sb.Append("</tbody>");
                string html = sb.ToString();
                CacheManager.AddCache(_cacheKey, html, 1);
                return html;
            }
            else
            {
                return obj as string;
            }
        }

        public string GetIndustryIndexString()
        {
            string _cacheKey = "IndustryIndexsKeyDataIssue";
            object obj = CacheManager.GetCache(_cacheKey);
            if (obj == null)
            {
                System.Text.StringBuilder sb = new System.Text.StringBuilder("");
                var industrys = Service.IndustryIndexSingleton.Instance.IndustryIndexList;
                //var stocklist = Service.SnapSingleton.Instance.SnapList;
                List<Entity.IndustryIndex> lists = industrys.Where(a => a.Level != null).Where(a => (a.Level == 1 && !a.Name.Equals("制造业")) || (a.Level == 2 && a.Code.StartsWith("c"))).OrderByDescending(a => a.UpDownRate).Take(8).ToList();
                for (int i = 0; i < lists.Count; i++)
                {
                    string name = lists[i].Name.Replace("、", "").Length > 7 ? lists[i].Name.Replace("、", "").Substring(0, 7) : lists[i].Name.Replace("、", "");
                    sb.Append("<tr>");
                    sb.AppendFormat("<td class=\"leftblock2\" width=\"85\"><a href=\"http://quote.stockstar.com/stock/industry_{1}.shtml\" title=\"{0}\">{0}</a></td>", name, lists[i].Code);
                    sb.AppendFormat("<td class=\"ts1\"><span class=\"{1} space\">{0:N2}%</span></td>", lists[i].UpDownRate, GetColor(Convert.ToDouble(lists[i].UpDownRate)));
                    //var query = stocklist.Where(a => !string.IsNullOrEmpty(a.IndustryCode)).Where(a => a.IndustryCode.Equals(lists[i].Code)).OrderByDescending(a => a.UpDownRate);
                    sb.AppendFormat("<td class=\"ts2\">{0}</td>", lists[i].UpQuantity);
                    sb.AppendFormat("<td class=\"ts7\">{0}</td>", lists[i].DownQuantity);
                    sb.Append("</tr>");
                }
                string html = sb.ToString();
                CacheManager.AddCache(_cacheKey, html, 1);
                return html;
            }
            else
            {
                return obj as string;
            }
        }

        public string GetMarketStocksString(int marketId)
        {

            string _cacheKey = "MarketStocksKeyDataIssue" + marketId;
            object obj = CacheManager.GetCache(_cacheKey);
            if (obj == null)
            {
                List<Entity.QuoteSnap> lists = Service.SnapSingleton.Instance.SnapList.Where(a => a.MarketType == marketId).Where(a => a.SecurtyType == 101 || a.SecurtyType == 103 || a.SecurtyType == 104).OrderByDescending(a => a.UpDownRate).Take(8).ToList();
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                foreach (var query in lists)
                {
                    sb.Append("<tr>");
                    sb.AppendFormat("<td class=\"leftblock2\"><a href=\"http://stock.quote.stockstar.com/{0}.shtml\">{1}</a></td>", query.Code, query.Name);
                    sb.AppendFormat("<td class='ts1'><span class=\"{0} space\">{1:N2}</span></td>", GetColor(Convert.ToDouble(query.UpDownRate)), Convert.ToDouble(query.CurrentPrice));
                    sb.AppendFormat("<td class='ts2'><span class=\"{0} space\">{1:N2}%</span></td>", GetColor(Convert.ToDouble(query.UpDownRate)), Convert.ToDouble(query.UpDownRate * 100));
                    sb.AppendFormat("<td><a href=\"http://stock.quote.stockstar.com/capital_{0}.shtml\" class=\"ico1\"></a></td>", query.Code);
                    sb.Append("</tr>");
                }
                string html = sb.ToString();
                CacheManager.AddCache(_cacheKey, html, 1);
                return html;
            }
            else
            {
                return obj as string;
            }
        }
        /// <summary>
        /// 头条新闻
        /// </summary>
        /// <returns></returns>
        public string GetTopNewsString()
        {
            string _cacheKey = "TopNewsDataIssue";
            object obj = CacheManager.GetCache(_cacheKey);
            if (obj == null)
            {
                System.Text.StringBuilder str = new System.Text.StringBuilder();
                NewsComponent nc = new NewsComponent();
                int count = nc.GetNewsByColumn(4008, 1, 2);
                if (count > 0)
                {
                    str.AppendLine("<h1><a href='http://stock.stockstar.com/" + nc.GetNewsID().Replace(",", "") + ".shtml'>" + GetLengthString(nc.GetSubject(), 17) + "</a></h1>");
                    //str.AppendLine("<h1><a href='http://stock.stockstar.com/" + nc.GetNewsID().Replace(",", "") + ".shtml'>我我我我我我我我我我我我我我我我</a></h1>");
                    str.AppendLine("<div class='news'>" + GetLengthString(nc.GetDigest(), 150) + "<a href='http://stock.stockstar.com/" + nc.GetNewsID().Replace(",", "") + ".shtml'>[详细内容]</a></div>");
                }
                else
                {
                    //    throw new Exception(count.ToString());
                }
                string html = str.ToString();
                CacheManager.AddCache(_cacheKey, html, 1);
                return html;
            }
            else
            {
                return obj as string;
            }
        }

        /// <summary>
        /// 获取现金流HTML
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public string GetCashProfitFlowString(string type)
        {
            string _cacheKey = "CashProfitFlowDataIssue" + type;
            object obj = CacheManager.GetCache(_cacheKey);
            if (obj == null)
            {
                System.Text.StringBuilder sb = new System.Text.StringBuilder("");
                List<Entity.CashFlow> lists = null;
                //  var stocklist = Service.SnapSingleton.Instance.SnapList.Where(a => !string.IsNullOrEmpty(a.IndustryCode)).OrderBy(a => a.IndustryCode);
                if (type.Equals("A"))
                {
                    lists = new Service.CashFlowService().GetProfitFlow();
                }
                else if (type.Equals("B"))
                {
                    lists = new Service.CashFlowService().GetContinueFlow();
                }
                else
                {
                    lists = new Service.CashFlowService().GetIndurstryFlow();
                }
                for (int i = 0; i < lists.Count; i++)
                {
                    string name = lists[i].Name.Replace("、", "").Length > 8 ? lists[i].Name.Replace("、", "").Substring(0, 8) : lists[i].Name.Replace("、", "");
                    sb.Append("<tr class=\"tr1\">");
                    if (type.Equals("C"))
                    {
                        sb.AppendFormat("<td class=\" leftblock2\"><a href=\"http://stock.quote.stockstar.com/{1}.shtml\" title=\"{0}\">{0}</a></td>", name, lists[i].Code);
                    }
                    else
                    {
                        sb.AppendFormat("<td class=\" leftblock2\"><a href=\"http://stock.quote.stockstar.com/{1}.shtml\" title=\"{0}\">{0}</a></td>", name, lists[i].Code);
                    }
                    if (type.Equals("A") || type.Equals("C"))
                    {
                        if (type.Equals("A"))
                        {
                            sb.AppendFormat("<td class='ts1' style=\"width:80px\"><span class=\"{1} space\">{0:N2}</span></td>", lists[i].Profit, GetColor(Convert.ToDouble(lists[i].Profit)));

                            sb.AppendFormat("<td class=\"ts2\">{0:N2}%</td>", lists[i].Precent);
                        }
                        else
                        {
                            sb.AppendFormat("<td style=\"text-align:right;\"><span class=\"{1} space\">{0:N2}</span></td>", lists[i].Profit, GetColor(Convert.ToDouble(lists[i].Profit)));

                            sb.AppendFormat("<td class=\"ts19\">{0:N2}%</td>", lists[i].Precent);
                        }

                    }
                    else
                    {
                        sb.AppendFormat("<td class=\"ts1\">{0}</td>", lists[i].Days);
                        sb.AppendFormat("<td class=\"ts2\" style=\"width:80px\"><span class=\"{1} space\">{0:N2}</span></td>", lists[i].Profit, GetColor(Convert.ToDouble(lists[i].Profit)));

                    }
                    if (type.Equals("C"))
                    {

                    }
                    else
                    {
                        sb.AppendFormat("<td><a class=\"ico1\" href=\"http://stock.quote.stockstar.com/capital_{0}.shtml\"></a></td>", lists[i].Code);
                    }
                    //    
                    sb.Append("</tr>");
                }
                string html = sb.ToString();
                CacheManager.AddCache(_cacheKey, html, 1);
                return html;
            }
            else
            {
                return obj as string;
            }
        }
        /// <summary>
        /// 获取龙虎榜HTML
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public string GetLongHuBangString(string type)
        {
            string _cacheKey = "LongHuBangDataIssue" + type;
            object obj = CacheManager.GetCache(_cacheKey);
            if (obj == null)
            {
                System.Text.StringBuilder sb = new System.Text.StringBuilder("");
                List<Entity.StockSummary> lists = null;
                if (type.Equals("A"))
                {
                    lists = new Service.StockSummaryService().GetStocks();
                }
                else if (type.Equals("B"))
                {
                    lists = new Service.StockSummaryService().GetYinYeTing();
                }
                else
                {
                    lists = new Service.StockSummaryService().GetJiGou();
                }
                for (int i = 0; i < lists.Count; i++)
                {
                    string name = lists[i].Name.Length > 4 ? lists[i].Name.Substring(0, 4) : lists[i].Name;
                    sb.Append("<tr class=\"tr1\">");
                    if (type.Equals("B"))
                    {
                        sb.AppendFormat("<td class=\"leftblock2\"><a href=\"http://quote.stockstar.com/BillBoard/department_" + lists[i].Code + "_1_1_1.html\">{0}</a></td>", name);
                    }
                    else
                    {
                        if (type.Equals("C"))
                        {
                            sb.AppendFormat("<td class=\"leftblock2\"><a href=\"http://stock.quote.stockstar.com/{1}.shtml\" title=\"{0}\">{0}</a></td>", name, lists[i].Code);

                        }
                        else {
                            sb.AppendFormat("<td class=\"leftblock2\"><a href=\"http://stock.quote.stockstar.com/{1}.shtml\" title=\"{0}\">{0}</a></td>", name, lists[i].Code);

                        }
                    }
                    sb.AppendFormat("<td class=\"ts1\"><span class=\"space\">{0:N2}</span></td>", lists[i].Account);
                    sb.AppendFormat("<td class=\"rightblock2\">{0}</td>", lists[i].Count);
                    string str = "明细";
                    if (type.Equals("C"))
                    {
                        str = "席位";
                    }
                    if (type.Equals("A"))
                    {
                        sb.AppendFormat("<td width=\"50\"><a href=\"http://stock.quote.stockstar.com/info/billboard_{0}.shtml\">{1}</a></td>", lists[i].Code, str);
                    }
                    else if (type.Equals("B"))
                    {
                        sb.AppendFormat("<td width=\"50\"><a href=\"http://quote.stockstar.com/BillBoard/department_" + lists[i].Code + "_1_1_1.html\">{0}</a></td>", str);
                    }
                    else
                    {
                        sb.AppendFormat("<td width=\"50\"><a href=\"http://stock.quote.stockstar.com/info/agentstatistic_{0}.shtml\">{1}</a></td>", lists[i].Code, str);
                    }
                    sb.Append("</tr>");
                }
                string html = sb.ToString();
                CacheManager.AddCache(_cacheKey, html, 1);
                return html;
            }
            else
            {
                return obj as string;
            }
        }
        /// <summary>
        /// 获取千股千评HTML
        /// </summary>
        /// <returns></returns>
        public string GetQianGuQianPingString()
        {
            string _cacheKey = "QianGuQianPingDataIssue";
            object obj = CacheManager.GetCache(_cacheKey);
            if (obj == null)
            {
                System.Text.StringBuilder sb = new System.Text.StringBuilder("");
                List<Entity.StockComment> lists = new Service.StockCommentService().GetStocks();
                for (int i = 0; i < lists.Count; i++)
                {
                    string name = lists[i].Name.Length > 6 ? lists[i].Name.Substring(0, 6) : lists[i].Name;
                    sb.Append("<tr class=\"tr1\">");
                    sb.AppendFormat("<td class=\"leftblock2\"><a href=\"http://stock.quote.stockstar.com/{1}.shtml\" title=\"{0}\">{0}</a></td>", name, lists[i].Code);
                    sb.AppendFormat("<td class=\"leftblock\">{0}</td>", lists[i].Comment);
                    sb.AppendFormat("<td><a href=\"http://stock.quote.stockstar.com/info/comment_{0}.shtml\" class=\"ico2\"></a></td>", lists[i].Code);
                    sb.Append("</tr>");
                }
                string html = sb.ToString();
                CacheManager.AddCache(_cacheKey, html, 1);
                return html;
            }
            else
            {
                return obj as string;
            }
        }
        /// <summary>
        /// 获取投资评级HTML
        /// </summary>
        /// <returns></returns>
        public string GetTouZiPingJiString()
        {
            string _cacheKey = "TouZiPingJiIssueDataIssue";
            object obj = CacheManager.GetCache(_cacheKey);
            if (obj == null)
            {
                System.Text.StringBuilder sb = new System.Text.StringBuilder("");
                List<Entity.StockComment> lists = new Service.StockCommentService().GetTouZiPingJi();
                for (int i = 0; i < lists.Count; i++)
                {
                    string name = lists[i].Name.Length > 6 ? lists[i].Name.Substring(0, 6) : lists[i].Name;
                    sb.Append("<tr class=\"tr1\">");
                    sb.AppendFormat("<td class=\"leftblock2\"><a href=\"http://stock.quote.stockstar.com/{1}.shtml\" title=\"{0}\">{0}</a></td>", name, lists[i].Code);
                    sb.AppendFormat("<td><a href=\"http://stock.stockstar.com/report/data_pool.htm\">{0}</a></td>", lists[i].YanJiuJiGou);
                    sb.AppendFormat("<td>{0}</td>", lists[i].Comment);
                    sb.AppendFormat("<td><a href=\"http://stock.quote.stockstar.com/corp/rating_{0}.shtml\" class=\"ico3\"></a></td>", lists[i].Code);
                    sb.Append("</tr>");
                }
                string html = sb.ToString();
                CacheManager.AddCache(_cacheKey, html, 1);
                return html;
            }
            else
            {
                return obj as string;
            }
        }
        /// <summary>
        /// 获取盈利预测HTML
        /// </summary>
        /// <returns></returns>
        public string GetYingLiYuCeString()
        {
            string _cacheKey = "YingLiYuCeDataIssue";
            object obj = CacheManager.GetCache(_cacheKey);
            if (obj == null)
            {
                System.Text.StringBuilder sb = new System.Text.StringBuilder("");
                List<Entity.StockComment> lists = new Service.StockCommentService().GetYingLiYuCe();
                for (int i = 0; i < lists.Count; i++)
                {
                    string name = lists[i].Name.Length > 6 ? lists[i].Name.Substring(0, 6) : lists[i].Name;
                    sb.Append("<tr class=\"tr1\">");
                    sb.AppendFormat("<td class=\"leftblock2\"><a href=\"http://stock.quote.stockstar.com/{1}.shtml\" title=\"{0}\">{0}</a></td>", name, lists[i].Code);
                    sb.AppendFormat("<td class=\"{1} rigthblock3\">{0:N2}</td>", lists[i].ProfitOn12, GetColor(lists[i].ProfitOn12));
                    sb.AppendFormat("<td class=\"{1} rigthblock3\">{0:N2}</td>", lists[i].ProfitOn13, GetColor(lists[i].ProfitOn13));
                    sb.AppendFormat("<td class=\"{2} rigthblock3\">{1:N2}</td>", lists[i].Code, lists[i].ProfitOn14, GetColor(lists[i].ProfitOn14));
                    sb.AppendFormat("<td><a href=\"http://stock.quote.stockstar.com/finance/predict_{0}.shtml\" class=\"ico4\"></a></td>", lists[i].Code);
                    sb.Append("</tr>");
                }
                string html = sb.ToString();
                CacheManager.AddCache(_cacheKey, html, 1);
                return html;
            }
            else
            {
                return obj as string;
            }
        }

        /// <summary>
        /// 头部的指数行情的跳转路径
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        private string GetURL(string code)
        {
            if (code.Equals("000001") || code.Equals("399001") || code.Equals("000300") || code.Equals("399006"))
            {
                return "http://index.quote.stockstar.com/" + code + ".shtml";
            }
            else
            {
                return "http://forex.stockstar.com/foreignEx/globalquote.aspx";
            }
        }
        /// <summary>
        /// 文字样式
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        private string GetClass(string code)
        {
            if (code.Equals("000001"))
            {
                return "btn1";
            }
            else
            {
                return "btn";
            }
        }
        /// <summary>
        /// 获取涨或跌的方向
        /// </summary>
        /// <param name="range"></param>
        /// <returns></returns>
        private string GetForce(double range)
        {
            return range > 0 ? "▲" : (range < 0 ? "▼" : "");
        }

        private string GetColor(double range)
        {
            if (range > 0)
            {
                return "red_color";
            }
            else if (range < 0)
            {
                return "green_color";
            }
            else
            {
                return "";
            }
        }
        /// <summary>
        /// 截取指定长度的字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private static string GetLengthString(string str, int length)
        {
            if (str.Length > length)
                return str.Substring(0, length) + "...";
            else
                return str;
        }
        private string GetColor(string code, double range)
        {
            string color = "";
            switch (code)
            {
                case "000001":
                case "399001":
                case "000300":
                case "399006":
                    if (range > 0)
                    {
                        color = "red_color";
                    }
                    else if (range < 0)
                    {
                        color = "green_color";
                    }
                    break;
                case "FTLI":
                case "DQII":
                case "NSDI":
                case "JPII":
                    if (range < 0)
                    {
                        color = "red_color";
                    }
                    else if (range > 0)
                    {
                        color = "green_color";
                    }
                    break;
                default:
                    break;
            }
            return color;
        }
    }
}